﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using NUnit.Framework;

using Refix.Core.Options;

namespace Refix.Core.Tests.Options
{
    [TestFixture]
    public class OptionTest
    {
        [Test]
        public void TryParse_Empty_Queue_Returns_False_Without_Error()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            Assert.False(Option.TryParse(queue, out commandLineOptions),
                         "Option didn't return false for empty queue.");
            Assert.IsEmpty(builder.ToString(), "Option wrote to output for empty queue.");
        }

        [Test]
        public void TryParse_Returns_False_With_Unknown_Option()
        {
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("-avz");
            Assert.False(Option.TryParse(queue, out commandLineOptions),
                         "Option.TryParse didn't return false for unknown option.");
        }

        [Test]
        public void TryParse_Parses_Long_Form()
        {
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("--verbose");
            Assert.True(Option.TryParse(queue, out commandLineOptions),
                        "Option failed to parse '--verbose'.");
            Assert.AreEqual(1, commandLineOptions.Count(), "Option didn't parse a single option.");
            Assert.IsInstanceOf(typeof(Verbose), commandLineOptions.First(),
                                "Option failed to parse '--verbose' to type Verbose.");
            Assert.AreEqual(0, queue.Count, "Option failed to pop entry off queue.");
        }

        [Test]
        public void TryParse_Parses_Short_Form()
        {
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("-v");
            Assert.True(Option.TryParse(queue, out commandLineOptions),
                        "Option failed to parse '-v'.");
            Assert.AreEqual(1, commandLineOptions.Count(), "Option didn't parse a single option.");
            Assert.IsInstanceOf(typeof(Verbose), commandLineOptions.First(),
                                "Option failed to parse '-v' to type Verbose.");
            Assert.AreEqual(0, queue.Count, "Option failed to pop entry off queue.");
        }

        [Test]
        public void TryParse_Quiet_Option_Sets_Quiet()
        {
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("--quiet");
            Option.TryParse(queue, out commandLineOptions);
            Assert.AreEqual(OutputVerbosity.Quiet, OutputWriter.Verbository,
                            "Option didn't set verbosity level for Quiet option.");
        }

        [Test]
        public void TryParse_Unknown_Option_Returns_False_And_Reports_Error()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("--unknown");
            Assert.False(Option.TryParse(queue, out commandLineOptions),
                         "Option didn't return false for unknown option.");
            StringAssert.Contains("--unknown", builder.ToString(),
                                  "Option didn't report failure parsing unknown option.");
        }

        [Test]
        public void TryParse_Verbose_Option_Sets_Verbose()
        {
            IEnumerable<Option> commandLineOptions;
            var queue = new Queue<string>();
            queue.Enqueue("--verbose");
            Option.TryParse(queue, out commandLineOptions);
            Assert.AreEqual(OutputVerbosity.Verbose, OutputWriter.Verbository,
                            "Option didn't set verbosity level for Verbose option.");
        }
    }
}